﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Aribaccio;
using BDPAutodetect.Device;

namespace BDPAutodetect.Models
{
    /// <summary>
    /// 参数界限数据
    /// </summary>
    public partial class MParameterLimitData : ObservableModel
    {
        /// <summary>
        /// 标识/编号
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 参数ID
        /// </summary>
        public int ParameterID { get; set; }

        /// <summary>
        /// 指标信息
        /// </summary>
        [ObservableProperty]
        protected MTargetSlave? targetSlave;

        /// <summary>
        /// 界限/最小限（有范围的界限最小限）
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(LimitDisplay))]
        protected string? limit;

        /// <summary>
        /// 界限/最大限（有范围的界限最大限）
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(LimitDisplay))]
        protected string? limitTo;

        /// <summary>
        /// 检测时长界限/s
        /// </summary>
        [ObservableProperty]
        protected int limitTime;

        /// <summary>
        /// 检测时长界限/s
        /// </summary>
        [ObservableProperty]
        protected string limitTimeDisplay = string.Empty;

        /// <summary>
        /// 指标类型
        /// </summary>
        public DeviceTargetType TargetType => this.TargetSlave?.Target.TargetType ?? DeviceTargetType.None;
        /// <summary>
        /// 界限范围显示
        /// </summary>
        public string LimitDisplay => this.TargetSlave == null || this.TargetSlave.DataType == DataType.String
            ? (this.Limit ?? string.Empty)
            : $"{(this.LimitTo ?? string.Empty)} - {(this.Limit ?? string.Empty)}";

        /// <summary>
        /// 深度克隆
        /// </summary>
        /// <returns></returns>
        public MParameterLimitData CloneDeep()
        {
            return new MParameterLimitData()
            {
                ID = this.ID,
                ParameterID = this.ParameterID,
                TargetSlave = this.TargetSlave?.CloneDeep(),
                Limit = this.Limit,
                LimitTo = this.LimitTo,
                LimitTime = this.LimitTime,
                LimitTimeDisplay = this.LimitTime.ToString()
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public TParameterLimit CloneTData()
        {
            return new TParameterLimit()
            {
                ID = this.ID,
                ParameterID = this.ParameterID,
                TargetID = this.TargetSlave?.Target.ID ?? 0,
                Limit = this.Limit,
                LimitTo = this.LimitTo,
                LimitTime = this.LimitTime
            };
        }

    }
}
